﻿@page "/DeviceActions"
@using Gurux.DLMS.AMI.Shared.DIs
@using Gurux.DLMS.AMI.Shared.Enums
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Gurux.DLMS.AMI.Shared.DTOs
@using Gurux.DLMS.AMI.Shared.DTOs.Authentication
@using Gurux.DLMS.AMI.Shared.Rest
@using Microsoft.AspNetCore.SignalR.Client
@using Gurux.DLMS.AMI.Shared
@using Gurux.DLMS.AMI.Module
@using Gurux.DLMS.AMI.Client.Helpers

@attribute [Authorize(Roles = "Admin, DeviceManager, Device")]
@inject NavigationManager NavigationManager
@inject HttpClient Http
@inject IGXNotifier Notifier
@inject ConfirmBase Confirm
@implements IDisposable

@if (Title)
{
    <PageTitle>@Properties.Resources.Actions @SelectedDevice</PageTitle>
}
<AuthorizeView Roles=@GXRoles.ToString(GXRoles.Admin, GXRoles.ComponentViewGroupManager)>
    <MenuControl RightCorner="true">
        <ChildContent>
            <MenuItem Text="@Properties.Resources.Clear" Icon="oi oi-trash" OnClick="@OnClear" />
        </ChildContent>
    </MenuControl>
</AuthorizeView>

<GXTable @ref="table"
         Context="action"
         ItemsProvider="@GetItems"
         SelectionMode="SelectionMode.None"
         Filter="@Filter"
         ShowAllUsers="@Header"
         Columns="@Columns"
         ShowRemoved="false"
         OnSearch="@Updated">
    <FilterContent>
        <th>
            <input class="form-control" type="text" placeholder="@Properties.Resources.FilterByDevice"
                   @onchange="@((ChangeEventArgs __e) => filter.Device.Name = Convert.ToString(__e.Value))" />
        </th>
        <th>
            <input class="form-control" placeholder="按创建时间..."
                   type="datetime-local"
                   @onchange="@((ChangeEventArgs __e) => filter.CreationTime = Convert.ToDateTime(__e.Value))" />
        </th>
        <th>
            <!--Type-->
        </th>
        <th>
            <!--Data-->
        </th>
        <th>
            <!--Reply-->
        </th>
    </FilterContent>
    <MenuContent>
        <ContextMenuItem Text="@Properties.Resources.Show" Icon="oi oi-eye" OnClick="@OnEdit"></ContextMenuItem>
    </MenuContent>
    <HeaderContent>
        <th>@Properties.Resources.Name</th>
        <Th Id="CreationTime" SortMode="SortMode.Descending">@Properties.Resources.CreationTime</Th>
            <Th Id="Type">@Properties.Resources.Type</Th>
            <th>@Properties.Resources.Data</th>
        <th>@Properties.Resources.Reply</th>
    </HeaderContent>
    <ItemContent>
        <td>@action.Device.Name</td>
        <Td Link="@("DeviceAction/Edit/" + action.Id)">@action.CreationTime</Td>
            <td>@((DeviceActionType?)@action.Type)</td>
            <td>@action.Data</td>
        <td>@action.Reply</td>
    </ItemContent>
</GXTable>
<br />
<Confirm @ref="ClearConfirmation"
         ConfirmationChanged="OnClearConfirmation"
         ConfirmationTitle=@Properties.Resources.ConfirmDataClear
         AllowDelete="false"
         OkTitle="Clear"
         ConfirmationMessage="@Properties.Resources.AreYouSureYouWantToClearLog">
</Confirm>

@code {
    /// <summary>
    /// Action.
    /// </summary>
    [Parameter]
    public string? Action { get; set; }

    /// <summary>
    /// Selected device action ID.
    /// </summary>
    [Parameter]
    public Guid? Id { get; set; }

    /// <inheritdoc />
    public string Name
    {
        get
        {
            return Gurux.DLMS.AMI.Client.Properties.Resources.DeviceActions;
        }
    }

    /// <inheritdoc />
    public Type? ConfigurationUI
    {
        get
        {
            return null;
        }
    }

    /// <inheritdoc />
    public string? Icon
    {
        get
        {
            return "oi oi-warning";
        }
    }

    /// <summary>
    /// Amount of the device actions shown on the view.
    /// </summary>
    [Parameter]
    public int Count { get; set; } = 0;

    /// <summary>
    /// Is filter shown.
    /// </summary>
    [Parameter]
    public bool Filter { get; set; } = true;

    /// <summary>
    /// Is header shown.
    /// </summary>
    [Parameter]
    public bool Header { get; set; } = true;

    /// <summary>
    /// Is edit allowed.
    /// </summary>
    [Parameter]
    public bool CanEdit { get; set; } = true;

    /// <summary>
    /// Available columns.
    /// </summary>
    [Parameter]
    public string[]? Columns { get; set; }

    /// <summary>
    /// Is title shown.
    /// </summary>
    [Parameter]
    public bool Title { get; set; } = true;

    /// <summary>
    /// Device action log filter.
    /// </summary>
    private GXDeviceAction filter = new GXDeviceAction() { Device = new GXDevice() };
    /// <summary>
    /// User is verified before devce actions are cleared.
    /// </summary>
    protected ConfirmBase? ClearConfirmation;

    public string? SelectedDevice
    {
        get;
        private set;
    }

    /// <summary>
    /// Active item.
    /// </summary>
    public GXDeviceAction? Active
    {
        get
        {
            return table?.Active;
        }
    }

    /// <summary>
    /// Table reference.
    /// </summary>
    protected GXTable<GXDeviceAction>? table;

    /// <summary>
    /// Update table.
    /// </summary>
    protected async Task Updated()
    {
        try
        {
            if (table != null)
            {
                Notifier?.ClearStatus();
                await table.RefreshDataAsync(true);
            }
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
    }

    protected override async Task OnInitializedAsync()
    {
        //Wait until table is loaded. Don't remove this or table is null and last settings are not available.
        await Task.Delay(1);
        try
        {
            Notifier.ProgressStart();
            Notifier.ClearStatus();
            if (Notifier == null)
            {
                throw new ArgumentException(Properties.Resources.InvalidNotifier);
            }
            Notifier.ClearStatus();
            Notifier.On<IEnumerable<GXDeviceAction>>(this, nameof(IGXHubEvents.DeviceActionAdd), async (error) =>
            {
                await Updated();
            });
            Notifier.On<IEnumerable<GXDeviceAction>>(this, nameof(IGXHubEvents.DeviceActionClear), async (error) =>
            {
                await Updated();
            });
            Notifier.Clear();
            Notifier.UpdateButtons();
            if (table != null && Id != null)
            {
                //Get last selected item.
                table.Active = new GXDeviceAction() { Id = Id.Value };
            }
        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
    }

    private async ValueTask<ItemsProviderResult<GXDeviceAction>> GetItems(GXItemsProviderRequest request)
    {
        //Don't clear status or error is lost.
        Notifier?.ProgressStart();
        try
        {
            ListDeviceAction req = new ListDeviceAction()
                {
                    Index = request.StartIndex,
                    Count = request.Count,
                    Filter = filter,
                    OrderBy = request.OrderBy,
                    Descending = request.Descending,
                    AllUsers = request.ShowAllUserData
                };
            if (Count != 0)
            {
                req.Count = Count;
            }
            var ret = await Http.PostAsJson<ListDeviceActionResponse>("api/DeviceAction/List", req, request.CancellationToken);
            return new ItemsProviderResult<GXDeviceAction>(ret.Actions, ret.Count);
        }
        catch (TaskCanceledException)
        {
            //Let the table component handle this.
            throw;
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
        finally
        {
            Notifier?.ProgressEnd();
        }
        return default;
    }

    public void Dispose()
    {
        Notifier.RemoveListener(this);
    }

    /// <summary>
    /// Show device action.
    /// </summary>
    public void OnEdit()
    {
        try
        {
            Notifier.ClearStatus();
            if (table?.Active == null)
            {
                throw new Exception(Gurux.DLMS.AMI.Client.Properties.Resources.NoItemIsSelected);
            }
            ClientHelpers.NavigateTo(NavigationManager, Notifier, "/DeviceAction/Edit/" + table?.Active.Id);
        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
    }

    /// <summary>
    /// Clear the device actions.
    /// </summary>
    public void OnClear()
    {
        Notifier?.ClearStatus();
        ClearConfirmation?.Show();
    }

    /// <summary>
    /// Clear the system error list.
    /// </summary>
    public async Task OnClearConfirmation(ConfirmArgs args)
    {
        try
        {
            if (args.Confirm)
            {
                await Http.PostAsJson<ClearDeviceActionResponse>("api/DeviceAction/Clear",
                new ClearDeviceAction()
                    {
                        Delete = args.Delete
                    });
            }
        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
    }
}
